define(['jquery','pages/index/model','util'],function($,cfg,util) {
	var isInit=true,
	    refresh,
	    timer,
	    isShow=false,
	    quotation,
	    setLastPrice=function(newQuotation){
	    	var id;
	    	for(id in cfg.lastPrice){
	    	   if((!quotation&&newQuotation[id])||(newQuotation[id]&&quotation[id]&&newQuotation[id].close!=quotation[id].close)){
	    	   		cfg.lastPrice[id] =!quotation?newQuotation[id].close:quotation[id].close;
	    	   }
	    	}			
		},
		quotesToLocal=function(quotes){
			var  local={},
			     mindecimal,
			     row,
		         groupid;
        	for(groupid in quotes){
        	   if(!cfg.goods_obj[groupid][0])continue;
        	   mindecimal=cfg.goods_obj[groupid][0].mindecimal;
        	   row=quotes[groupid];
        	   local[groupid]={
        	   	 open:util.priceFormat(mindecimal,row.open),
        	   	 top:util.priceFormat(mindecimal,row.high),
        	   	 lower:util.priceFormat(mindecimal,row.low),
        	   	 close:util.priceFormat(mindecimal,row.close)			        	   
        	   }
        	}
        	return local;
		};
    return {
    	lastShowPrice:0,//
        init:function(){
        	if(!refresh)refresh=this;
        	isShow=true;
        	$.ajax({
		        url: cfg.proxy?util.url('hq'):'/index.php/site/refresh',
		        type: 'post',
		        data: {ordernum: cfg.ordernum},
		        dataType: 'json',
		        success: function(data) {
		        	var localQuotation;
		            //行情
		        	if(cfg.proxy&&data.retcode!=0){
		        		cfg.proxy=0;
		        	}else if(cfg.proxy&&data.retcode==0){
		        		localQuotation=quotesToLocal(data.quotes);
		        	}else{
		        		localQuotation=data.quotation;
		        	}
		        	if(localQuotation){
		        		setLastPrice(localQuotation);
			        	quotation=localQuotation;
			            refresh.updateRefreshUI();
		        	}
		            if(isShow)timer=setTimeout(refresh.init,cfg.refreshtime);
		        },
		        error: function() {
		            if(isShow)timer=setTimeout(refresh.init, cfg.refreshtime);
		
		        }
		    });
        },
        updateRefreshUI:function(){
        	var refresh=this;
        	refresh.updateCurrentPrice();
            refresh.updateTopLower();
            if(true){
            	refresh.updateOpenClose();
            	isInit=false;
            }
        },
        updateCurrentPrice:function(){
        	var groupid=cfg.groupid,
		        currentPrice=quotation[groupid]?quotation[groupid].close:'',
		        lastPrice=cfg.lastPrice[groupid],
		        $currentPriceCt=$('#currentPrice'),
		        isUp,
		        removeCls,
		        addCls;
 
            if(this.lastShowPrice!=currentPrice){
            	this.changeType=false;
            	isUp=currentPrice>=lastPrice;
	            removeCls=isUp?'down':'up';
                addCls=!isUp?'down':'up';
	            $currentPriceCt.html(currentPrice+(isUp?'↑':'↓')).removeClass(removeCls).addClass(addCls);
	            this.lastShowPrice=currentPrice;
            }
        },
        updateTopLower:function(){
        	var price=quotation[cfg.groupid];
        	price=price?price:{top:'',lower:''};
        	$('#topLowerCt').html('最高：'+price.top+'<br/>最低：'+price.lower);
        },
        updateOpenClose:function(){
        	var price=quotation[cfg.groupid];
        	price=price?price:{close:'',open:''};
            $('#openClostCt').html('当前：'+price.close+'<br/>今开：'+price.open);
        },
        clearTimer:function(){
        	clearTimeout(timer);
        	isShow=false;
        }
    }
});